The major new XFun hook in version 2.4 is the ability to access events. This allows XFuns to display windows etc.
------------- Intercepting events
The callback:
void InstallEventHandler(funptr handler)
Installs and external event handler of the form:
short doevent(EventRecord *theEvent)
MathPad calls this handler for every (non-null) event. If the handler returns TRUE, MathPad does no processing on the event. If the handler returns FALSE MathPad will attempt to handle the event. The handler must process ALL events pertaining to its windows and return TRUE. Events that are not handled by the XFun should return FALSE. The XFun must install at least one function before calling InstallEventHandler().
Allows the XFun to maintain a different cursor in its window. This routine MUST be called in response to a mouse moved event when the XFun window is in front. If the XFun does not need to change the cursor it can pass a NULL region. If the XFun does change the cursor it must also call this routine whenever the region changes.
void DoMenuItem(long selector);
Perform the action for a MathPad menu item. This allows the XFun event handler to call the toolbox MenuSelect(theEvent->where), intercept items it wants and pass back other selections. MathPad assumes that menu selections come from user interaction so XFuns could get into trouble if they arbitrarilly force menu selections. For example if the XFun selected a disabled menu item, disaster is likely.
short GetDocInfo(long *docref,FSSpec *fspec,WindowPtr *plotwindow);
Gets a couple of pieces of info about the document being evaluated. The file spec can be used to save XFun settings in the document's resource fork. The reference number "docref" can be used to check if the current document is the same as the one that last called the XFun. It returns false if there is no open document.
------------- Messages from MathPad
To enable a little more sophistcated interaction between MathPad and an XFun, MathPad sends a few private messages to XFun event handlers. Messages use the app3Evt type and put a message code in theEvent->message. The document associated with the message can be found by calling GetDocInfo().
The following message codes are defined for 2.4.3 in "xfuns.h"
DOCNEW -- a new document has been created
DOCOPENED -- a document has been opened and read in
DOCCHANGED -- a different document has been selected
DOCCLOSE -- a document is being closed
DOCSAVED -- a document has been saved
PLOTUPDATE -- the plot traces have been re-drawn. The grafport is set.
APPQUIT -- MathPad is quitting
BEGINPARSE -- starting to evaluate
ENDPARSE -- finished evaluating
The XFun event handler is not required to handle any messages. It should return FALSE for an app3Evt (if it is used or not) so that the message will be passed on to the next XFun.
---------------------------
This folder contains two example CodeWarrior projects using event handlers:
scatter68K. -- XFun that creates its own window and handles events
scatter src: scatter.c
scatter src: doevent.c
util src: callbackg.c
util src: entry68K.c
<MacOS.lib>
sliders68K. -- XFun that creates slider bar controls